package data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;

public class Item {

	private Type type;
	private Vector<Prefix> prefixes;
	private Vector<Adjectif> adjectives;
	private Vector<Suffix> suffixes;
	private HashMap<String, Integer> characStats;
	private List<String> miscStats;

	public Item(Type type, Vector<Prefix> prefixes,
			Vector<Adjectif> adjectives, Vector<Suffix> suffixes) {
		super();
		this.type = type;
		this.prefixes = prefixes;
		this.adjectives = adjectives;
		this.suffixes = suffixes;
		
		characStats = new HashMap<String, Integer>();
		miscStats = new ArrayList<String>();
		
		addStats();
		
	}

	@Override
	public String toString() {
		StringBuffer b = new StringBuffer();
		// Prefixes Type Adjectives Suffixes
		for (Prefix p : prefixes)
			b.append(p.getName(type.getGender()) + " ");
		b.append(type.getName() + " ");
		for (Adjectif a : adjectives)
			b.append(a.getName(type.getGender()) + " ");
		for (Suffix s : suffixes) {
			b.append(s.getName(type.getGender()) + " ");
			b.append(s.getAdjectif().getName(s.getGender()) + " ");
		}
		b.append("\n");

		if(characStats != null)
			b.append(displayCharacStats(characStats));
		if(miscStats != null)
			b.append(displayMiscStats(miscStats));
		b.append("\n");
		
//		for (Prefix p : prefixes)
//			b.append(p.getBonus() + "\n");
//		for (Adjectif a : adjectives)
//			b.append(a.getBonus() + "\n");
//		for (Suffix s : suffixes)
//			b.append(s.getBonus() + "\n");
		return b.toString();
	}
	
	private String displayCharacStats(HashMap<String, Integer> map) {
		StringBuffer sb = new StringBuffer();
		for(String charac : map.keySet()) {
			sb.append(charac + " ");
			if(map.get(charac) >= 0) {
				sb.append("+");
			} else {
				//sb.append("-");
			}
			sb.append(map.get(charac) + "\n");
		}
		
		return sb.toString();
	}
	
	private String displayMiscStats(List<String> list) {
		StringBuffer sb = new StringBuffer();
		for(String stat : list) {
			sb.append(stat + "\n");
		}
		
		return sb.toString();
	}

	private void addStats() {
		//Add stats of the piece of stuff
		if(type != null) {
			if( type.getCharacStats() != null ) {
				for(String key : type.getCharacStats().keySet()) {
					if(characStats.containsKey(key)) {
						characStats.put(key, characStats.get(key) + type.getCharacStats().get(key));
		    		} else {
		    			characStats.put(key, type.getCharacStats().get(key));
		    		}
				}
			}
			
			if(type.getMiscStats() != null) {
				for(String str : type.getMiscStats()) {
					miscStats.add(str);
				}
			}
		}
		
		//Add stats of the prefix
		if(prefixes != null) {
			for(Prefix p : prefixes) {
				if( p.getCharacStats() != null ) {
					for(String key : p.getCharacStats().keySet()) {
						if(characStats.containsKey(key)) {
							characStats.put(key, characStats.get(key) + p.getCharacStats().get(key));
			    		} else {
			    			characStats.put(key, p.getCharacStats().get(key));
			    		}
					}
				}
				
				if(p.getMiscStats() != null) {
					for(String str : p.getMiscStats()) {
						miscStats.add(str);
					}
				}
			}
		}
		
		//Add stats of the adjectives after the piece
		if(adjectives != null) {
			for(Adjectif a : adjectives) {
				if( a.getCharacStats() != null ) {
					for(String key : a.getCharacStats().keySet()) {
						if(characStats.containsKey(key)) {
							characStats.put(key, characStats.get(key) + a.getCharacStats().get(key));
			    		} else {
			    			characStats.put(key, a.getCharacStats().get(key));
			    		}
					}
				}
				
				if(a.getMiscStats() != null) {
					for(String s : a.getMiscStats()) {
						miscStats.add(s);
					}
				}
			}
		}
		
		//Add stats of the suffix and it's adjectif
		if(suffixes != null) {
			for(Suffix s : suffixes) {
				if(s.getAdjectif() != null) {
					Adjectif a = s.getAdjectif();
					if( a.getCharacStats() != null ) {
						for(String key : a.getCharacStats().keySet()) {
							if(characStats.containsKey(key)) {
								characStats.put(key, characStats.get(key) + a.getCharacStats().get(key));
				    		} else {
				    			characStats.put(key, a.getCharacStats().get(key));
				    		}
						}
					}
					
					if(a.getMiscStats() != null) {
						for(String str : a.getMiscStats()) {
							miscStats.add(str);
						}
					}
				}
				
				
				if( s.getCharacStats() != null ) {
					for(String key : s.getCharacStats().keySet()) {
						if(characStats.containsKey(key)) {
							characStats.put(key, characStats.get(key) + s.getCharacStats().get(key));
			    		} else {
			    			characStats.put(key, s.getCharacStats().get(key));
			    		}
					}
				}
				
				if(s.getMiscStats() != null) {
					for(String str : s.getMiscStats()) {
						miscStats.add(str);
					}
				}
			}
		}
	}
}
